package com.example.ams.controller;

import com.example.ams.dto.TravelRecordDTO;
import com.example.ams.service.TravelRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 出差记录管理控制器
 * Travel Record Controller
 */
@RestController
@RequestMapping("/api/travels")
@CrossOrigin(origins = "*")
public class TravelRecordController {

    @Autowired
    private TravelRecordService travelRecordService;

    /**
     * 查询出差记录
     * GET /api/travels?empId=&from=&to=
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> getTravelRecords(
            @RequestParam(required = false) Long empId,
            @RequestParam(required = false) String from,
            @RequestParam(required = false) String to) {

        try {
            List<TravelRecordDTO> records = travelRecordService.findByConditions(empId, from, to);

            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "success");
            response.put("data", records);
            response.put("total", records.size());

            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
        }
    }

    /**
     * 根据ID查询出差记录
     * GET /api/travels/{id}
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getTravelRecordById(@PathVariable Long id) {
        try {
            TravelRecordDTO record = travelRecordService.findById(id);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "success");
            response.put("data", record);
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 404);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 新增出差记录
     * POST /api/travels
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> createTravelRecord(
            @Validated @RequestBody TravelRecordDTO recordDTO) {
        try {
            TravelRecordDTO created = travelRecordService.create(recordDTO);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 201);
            response.put("message", "出差记录创建成功");
            response.put("data", created);
            return ResponseEntity.status(HttpStatus.CREATED).body(response);
        } catch (RuntimeException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
        }
    }

    /**
     * 删除出差记录
     * DELETE /api/travels/{id}
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteTravelRecord(@PathVariable Long id) {
        try {
            travelRecordService.delete(id);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "出差记录删除成功");
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
        }
    }
}

